package org.pk.preferences;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;

import com.cs.pk.Activator;
import com.cs.pk.utils.Constants;

public class NDSPreferences extends PreferencePage implements IWorkbenchPreferencePage {

	
	private IPreferenceStore store = null; 
	
	private String csUrl,homeUrl = null;
	
	private Text urlSpace,homeSpace = null;
	
	public NDSPreferences() {
		
	}

	public NDSPreferences(String title) {
		super(title);
	}

	public NDSPreferences(String title, ImageDescriptor image) {
		super(title, image);
	}

	protected Control createContents(Composite parent) {
		return createURLBOX(parent);
	}
	
	private Control createURLBOX(Composite parent) {
		Group url = new Group(parent,SWT.NONE);
		GridLayout gl = new GridLayout(2,false);
		url.setLayout(gl);
		
		Label homeUrlLabel = new Label(url,SWT.LEFT);
		homeUrlLabel.setText("Home Url :");
		
		homeSpace = new Text(url,SWT.SINGLE|SWT.BORDER);
		homeSpace.setText(homeUrl);
		GridData hgd = new GridData(GridData.FILL_HORIZONTAL);
		homeSpace.setLayoutData(hgd);
		
		Label csUrlLabel = new Label(url,SWT.LEFT);
		csUrlLabel.setText("Code Striker Url :");
		
		urlSpace = new Text(url,SWT.SINGLE|SWT.BORDER);
		urlSpace.setText(csUrl);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		urlSpace.setLayoutData(gd);
		return url;
	}
	
	protected IPreferenceStore doGetPreferenceStore() {
	        return getPreferenceStore();
	}
	
	public void init(IWorkbench workbench) {
           setPreferenceStore(PlatformUI.getPreferenceStore());
           store = getPreferenceStore();
           csUrl = store.getString(Constants.CODESTRIKERURL);
           homeUrl = store.getString(Constants.HOMEURL);
	}
	
	public boolean performOk() {
		getPreferenceStore().setValue(Constants.CODESTRIKERURL, urlSpace.getText().trim());
		getPreferenceStore().setValue(Constants.HOMEURL, homeSpace.getText().trim());
		Activator.getDefault().savePluginPreferences();
		return super.performOk();
	}
	
	protected void performDefaults() {  
		urlSpace.setText(Activator.getDefault().getDefaultPreference(Constants.CODESTRIKERURL));
		homeSpace.setText(Activator.getDefault().getDefaultPreference(Constants.HOMEURL));
		getPreferenceStore().setToDefault(Constants.CODESTRIKERURL);
		getPreferenceStore().setToDefault(Constants.HOMEURL);
		super.performDefaults();
    }

}
